package com.zoro.barn.business.service.system.business.menu.controller;

import com.zoro.barn.business.service.system.business.menu.entiity.BarnMenu;
import com.zoro.barn.business.service.system.business.menu.service.BarnMenuService;
import com.zoro.barn.commons.webcommons.tools.WebContentUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhaoxingwu
 */
@RestController
@RequestMapping("/menu")
public class BarnMenuController {

    private final BarnMenuService barnMenuService;

    public BarnMenuController(BarnMenuService barnMenuService) {
        this.barnMenuService = barnMenuService;
    }

    /**
     * 查询全部数据
     *
     * @return result set
     */
    @RequestMapping("/getAll")
    public List<BarnMenu> getAll() {
        return this.barnMenuService.getAll();
    }

    /**
     * 查询一条数据
     *
     * @param pid pid
     * @return data
     */
    @RequestMapping("/getById/{pid}")
    public BarnMenu getById(@PathVariable("pid") String pid) {
        return this.barnMenuService.getById(pid);
    }

    /**
     * 保存数据
     *
     * @param menu data
     * @return saved data
     */
    @RequestMapping("/save")
    public BarnMenu save(@RequestBody BarnMenu menu) {
        String userId = WebContentUtil.requestUserInfo().getPid();
        if (StringUtils.isBlank(menu.getPid())) {
            menu.setCreateId(userId);
            return this.barnMenuService.insert(menu);
        } else {
            menu.setUpdateId(userId);
            return this.barnMenuService.update(menu);
        }
    }

    /**
     * delete one record by primary id and all its children node
     *
     * @param pid pid
     */
    @RequestMapping("/delete/{pid}")
    public void delete(@PathVariable("pid") String pid) {
        this.barnMenuService.delete(pid);
    }
}
